package cn.buk.qms.web;

import cn.buk.api.wechat.dto.BaseResponse;
import cn.buk.api.wechat.entity.WeixinCustomMenu;
import cn.buk.api.wechat.entity.WeixinUser;
import cn.buk.api.wechat.service.WeixinService;
import cn.buk.common.JsonResult;
import cn.buk.common.dto.CommonDto;
import cn.buk.common.sc.CommonSearchCriteria;
import cn.buk.common.sc.Page;
import cn.buk.tms.entity.misc.Enterprise;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * 微信服务号相关的内容
 * Created by yfdai on 2019-04-08
 * @author yfdai
 */
@Profile("weixin-service")
@RestController
@RequestMapping("/admin/wxs")
public class AdminWxsController extends BaseController {

    @Autowired
    private WeixinService weixinService;


    @RequestMapping("/menus")
    public CommonDto<WeixinCustomMenu> searchCustomMenus(HttpServletRequest request) {
        final int enterpriseId0 = getEnterpriseId(request);

        int enterpriseId = -1;

        if (enterpriseId0 == Enterprise.ENTERPRISE_PLATFORM_ADMINISTRATOR) {
            enterpriseId = 0;
        } else if (enterpriseId0 > 0) {
            enterpriseId = enterpriseId0;
        }

        List<WeixinCustomMenu> menus = weixinService.searchCustomMenus(enterpriseId);

        CommonDto<WeixinCustomMenu> dto = new CommonDto<>();
        dto.setDataList(menus);
        dto.setPage(new Page());

        return dto;
    }

    /**
     * 在数据库中删除指定自定义菜单项
     * @param request
     * @return
     */
    @RequestMapping("/menu/{id}/delete")
    public JsonResult searchCustomMenus(HttpServletRequest request,
                                        @PathVariable("id") final int id) {
        final int enterpriseId = getEnterpriseId(request);

        return JsonResult.createJsonResult(weixinService.deleteCustomMenu(enterpriseId, id));
    }

    /**
     * 在数据库中创建新的自定义菜单项
     * @param request
     * @return
     */
    @RequestMapping("/menu/create")
    public JsonResult createCustomMenus(HttpServletRequest request,
                                        @RequestParam("name") final String name,
                                        @RequestParam(value = "orderNum", defaultValue = "0") final int orderNum,
                                        @RequestParam("type") final String type,
                                        @RequestParam("url") final String url,
                                        @RequestParam("key") final String key,
                                        @RequestParam("level") final int level,
                                        @RequestParam("parentId") final int parentId) {
        final int enterpriseId = getEnterpriseId(request);

        return JsonResult.createJsonResult(weixinService.createCustomMenu(enterpriseId, name, orderNum, type, url, key, level, parentId));
    }

    /**
     * 上传自定义菜单
     * @param request
     * @return
     */
    @RequestMapping("/menus/upload")
    public JsonResult uploadCustomMenus(HttpServletRequest request) {
        final int enterpriseId = getEnterpriseId(request);

        try {
            BaseResponse baseResponse = weixinService.uploadCustomMenu(enterpriseId);

            return baseResponse.getErrcode() == 0 ? JsonResult.createJsonResult(1) : JsonResult.createJsonResult(-1, baseResponse.getErrmsg());
        } catch (Exception ex) {
            ex.printStackTrace();
            return JsonResult.createJsonResult(-1, "错误：请联系管理员配置系统参数(" + ex.getMessage() + ")");
        }
    }


    @GetMapping("/users")
    public CommonDto<WeixinUser> searchSubscribers(HttpServletRequest request,
                                                   @ModelAttribute("sc") CommonSearchCriteria sc) {
        final int enterpriseId = getEnterpriseId(request);

        CommonSearchCriteria sc1 = new CommonSearchCriteria();

        BeanUtils.copyProperties(sc, sc1);
        sc1.setOrderBy(1);
        List<WeixinUser> users = weixinService.searchSubscribers(enterpriseId, sc1);
        BeanUtils.copyProperties(sc1, sc);

//        users = users.stream().sorted(Comparator.comparing(WeixinUser::getSubscribe_time).reversed()).collect(Collectors.toList());

        CommonDto<WeixinUser> dto = new CommonDto<>();
        dto.setDataList(users);
        dto.setPage(sc.getPage());

        return dto;
    }

    @PostMapping("/users/sync")
    public JsonResult syncUserList(HttpServletRequest request) {
        return JsonResult.createJsonResult(weixinService.syncUserList(getEnterpriseId(request)));
    }
}
